﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 易容酒店管理系统
{
    public partial class check_out : Form
    {
        public check_out()
        {
            InitializeComponent();
        }
        //读取用户信息
        public void Lookup()
        {
            string Sql = "select Customer_data.Name,Customer_data.Document_type,Customer_data.CAAC,Customer_data.Telephone,Guest_Room_Number.PrimaryID,Guest_Room_Number.[Type],Guest_Room_Number.InMoney from Guest_Room_Number,Customer_data where Guest_Room_Number.RoomID='" + RoomID.Text + "'and Guest_Room_Number.Guest_information=Customer_data.Name;";
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    SqlDataReader red = cmd.ExecuteReader();
                    if (red.HasRows)
                    {
                        red.Read();
                        txtName.Text = red[0].ToString();
                        txtPapers.Text = red[1].ToString();
                        txtID.Text = red[2].ToString();
                        txtTelephone.Text = red[3].ToString();
                        txtCheckID.Text = red[4].ToString();
                        txtRoomType.Text = red[5].ToString();
                        Money = Convert.ToInt32(red[6]);
                    }
                }
            }
        }
        int Money =0;
        //退房
        private void button1_Click(object sender, EventArgs e)
        {
            #region 修改
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "Delete from Customer_data where CAAC ='" + txtID.Text + "';";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    int judge = cmd.ExecuteNonQuery();
                    if (judge >0)
                    {
                        //
                    }
                    con.Close();
                }
                string Sql2 = "Delete from Guest_Room_Number  where Telephone='" + txtTelephone.Text + "';";
                using (SqlCommand cmd2 = new SqlCommand(Sql2, con))
                {
                    con.Open();
                    int judge = cmd2.ExecuteNonQuery();
                    if (judge > 0)
                    {
                        //
                    }
                    con.Close();
                }
                string Sql3 = "update GuestInfo set GusetInfo='无',setting='未打扫' where RoomID='" + RoomID.Text + "';";
                using (SqlCommand cmd3 = new SqlCommand(Sql3, con))
                {
                    con.Open();
                    int judge = cmd3.ExecuteNonQuery();
                    if (judge > 0)
                    {
                        //
                    }
                    con.Close();
                }
                string Sql4 = "insert into Finance values('" + txtName.Text + "','" + txtPapers.Text + "','" + txtID.Text + "','" + txtTelephone.Text + "','" + txtCheckID.Text + "','" + txtRoomType.Text + "','" + Money.ToString() + "','"+comboBox1.Text+"');";
                using (SqlCommand cmd4 = new SqlCommand(Sql4, con))
                {
                    con.Open();
                    int judge = cmd4.ExecuteNonQuery();
                    if (judge > 0)
                    {
                        //
                    }
                    con.Close();
                }
                string Sql5 = "select Money from Guest_Room where GuestType='" + txtRoomType.Text + "';";
                int Moneys = 0;
                int i = 0;
                using (SqlCommand cmd5 = new SqlCommand(Sql5, con))
                {
                    con.Open();
                    i = Convert.ToInt32(cmd5.ExecuteScalar());
                    Moneys += Convert.ToInt32(cmd5.ExecuteScalar());
                    con.Close();
                }
                string Sql6= "select InMoney from Guest_Room_Number where PrimaryID='"+txtCheckID.Text+"';";
                using (SqlCommand cmd6 = new SqlCommand(Sql6, con))
                {
                    con.Open();
                    Moneys += Convert.ToInt32(cmd6.ExecuteScalar());
                    con.Close();
                }
                string Sql7 = "select Money from Method where Method=(select Method from Customer_Reserve where Telephone='" + txtTelephone.Text + "');";
                using (SqlCommand cmd7 = new SqlCommand(Sql7, con))
                {
                    con.Open();
                    Moneys += Convert.ToInt32(cmd7.ExecuteScalar());
                    con.Close();
                }
                MessageBox.Show("应付" + Moneys.ToString() + "并退回" + i);
            }
            #endregion
            
        }
        //查询
        private void button2_Click(object sender, EventArgs e)
        {
            Lookup();
        }

        private void picColse_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

